package com.zrf.geekbang.spring.ioc.overview.dependency.injection;

import com.zrf.geekbang.spring.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

/**
 * 依赖查找实例
 * @author zhouruifeng
 * @date 13.6.20
 */
public class DependencyInjectionDemo {

    public static void main(String[] args) {
        //配置xml配置文件
        //启动spring上下文
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-injection-context.xml");
        UserRepository userRepository = beanFactory.getBean(UserRepository.class);
        System.out.println(userRepository.getBeanFactory());
        System.out.println(userRepository.getBeanFactory() == beanFactory);

        System.out.println(userRepository.getUserObjectFactory().getObject());
        System.out.println(userRepository.getObjectFactory().getObject());
        System.out.println(userRepository.getObjectFactory().getObject() == beanFactory);

        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println(environment);
    }

    private void whoIoc(UserRepository userRepository, BeanFactory beanFactory) {
        // beanFactory == applicationContext
        System.out.println(userRepository.getBeanFactory() == beanFactory);
    }

}
